﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Threading;
using FB2er.Properties;

namespace FB2er.ViewModel
{
    public class MainVM : INotifyPropertyChanged
    {
        private readonly FlowDocument _doc;
        private readonly FlowDocumentReader _rdr;

        public MainVM(FlowDocumentReader rdr)
        {
            _rdr = rdr;
            _doc = rdr.Document;
        }

        public void LoadBook(string fileName, int pageNumber)
        {
            try
            {
                try
                {
                    FB2Parser.LoadFB2(fileName, _doc);
//                    new FB2Loader().LoadFB2(fileName, _doc);
//                    Action a = () => NavigationCommands.FirstPage.Execute(null, _rdr);
                    Action a = () => NavigationCommands.GoToPage.Execute(pageNumber, _doc);
                    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ContextIdle, a);
                    Title = fileName;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    fileName = "";
                }
            }
            finally
            {
                if (!string.IsNullOrEmpty(fileName))
                {
                    if (Settings.Default.RecentBooks == null)
                        Settings.Default.RecentBooks = new StringCollection();
                    Settings.Default.RecentBooks.Remove(fileName);
                    Settings.Default.RecentBooks.Insert(0, fileName);
                    while (Settings.Default.RecentBooks.Count > 10) { Settings.Default.RecentBooks.RemoveAt(10); }
                    Settings.Default.Save();
                    RaisePropertyChanged("RecentBooks");
                }
            }
        }

        public IEnumerable<string> RecentBooks
        {
            get
            {
                return Settings.Default.RecentBooks == null ? null : Settings.Default.RecentBooks.Cast<string>();
            }
        }

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        public void LoadSettings()
        {
            _rdr.ViewingMode = Settings.Default.LastViewingMode;
            _rdr.Zoom = Settings.Default.LastZoom;
        }

        public void SaveSettings()
        {
            Settings.Default.LastViewingMode = _rdr.ViewingMode;
            Settings.Default.LastZoom = _rdr.Zoom;
            Settings.Default.LastPageNumber = _rdr.PageNumber;
            Settings.Default.Save();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propName));
        }
    }
}
